#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash Augustus-3.4.0.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1
if [[ "$1" == "$2" ]]; then echo "构建路径与安装路径需为不同路径，请输入正确的路径"; exit 1; fi

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
yum install -y  environment-modules csh time patch libatomic perl-Data-Dumper autoconf automake libtool numactl binutils systemd-devel valgrind flex wget

set -e
wget -t 10 -c -P $buildpath https://github.com/Gaius-Augustus/Augustus/archive/refs/tags/v3.4.0.tar.gz
tar xf $buildpath/v3.4.0.tar.gz -C $buildpath && rm -rf $buildpath/v3.4.0.tar.gz

cd $buildpath/Augustus-3.4.0

echo "--修改/usr/include/bamtools--"
sed -i 's|/usr/include/bamtools|${installpath}/AUGUSTUS/bamtools-2.5.2/include/bamtools|g' `grep -rl "/usr/include/bamtools" ./`
sed -i 's|/opt/augustus|'$installpath'/AUGUSTUS/Augustus|g' Makefile

echo "--修改common.mk文件--"
#修改AUGVERSION = 3.4.0  
sed -i "s/3.3.3/3.4.0/g" common.mk

echo "--修改bam2hints的makefile--"
#修改bam2hints的makefile
cd $buildpath/Augustus-3.4.0/auxprogs/bam2hints
sed -i 's|INCLUDES = -I${installpath}/AUGUSTUS/bamtools-2.5.2/include/bamtools|INCLUDES = -I/$(BAMTOOLS)/include/bamtools|g' Makefile
sed -i 's|LIBS = -lbamtools -lz|LIBS = $(BAMTOOLS)/lib64/libbamtools.a -lz |g' Makefile

echo "--修改filterBam/src的makefile--"
#修改filterBam/src的makefile
cd $buildpath/Augustus-3.4.0/auxprogs/filterBam/src
sed -i 's|BAMTOOLS = ${installpath}/AUGUSTUS/bamtools/include/bamtools|\#BAMTOOLS = ${installpath}/AUGUSTUS/bamtools/include/bamtools|g' Makefile
sed -i 's|INCLUDES = -I$(BAMTOOLS) -Iheaders|INCLUDES = -I$(BAMTOOLS)/include/bamtools -Iheaders|g' Makefile
sed -i 's|LIBS = -lbamtools|LIBS = $(BAMTOOLS)/lib64/libbamtools.a|g' Makefile

echo "--修改Augustus-3.4.0/src的makefile--"
#修改Augustus-3.4.0/src的makefile
cd $buildpath/Augustus-3.4.0/src
sed -i 's|INCLS += -I/usr/include/lpsolve|INCLS += -I'$installpath'/AUGUSTUS/lp_solve_5.5/|g' Makefile
sed -i 's|LIBS += -llpsolve55 -lcolamd -ldl|LIBS += -L'$installpath'/AUGUSTUS/lp_solve_5.5/lpsolve55/bin/ux64/ -llpsolve55 -lcolamd -ldl|g' Makefile
sed -i 's|INCLS    += -I/usr/include/mysql -I/usr/include/mysql++|INCLS    += -I/usr/include/mysql -I'$installpath'/AUGUSTUS/mysql++-3.3.0/include/mysql++/|g' Makefile
sed -i 's|LIBS     += -lmysqlclient -lmysqlpp|LIBS     += -L'$installpath'/AUGUSTUS/mysql++-3.3.0/lib -L/usr/lib64/mysql/ -lmysqlclient -lmysqlpp|g' Makefile


cd $buildpath/Augustus-3.4.0
echo "--make start--"
CXX=clang++ CC=clang make -j$(nproc) BAMTOOLS=${installpath}/AUGUSTUS/bamtools-2.5.2 TOOLDIR=$installpath/AUGUSTUS LD_LIBRARY_PATH=$installpath/AUGUSTUS/bamtools-2.5.2/lib64:$LD_LIBRARY_PATH LIBRARY_PATH=$installpath/AUGUSTUS/bamtools-2.5.2/lib64:$LIBRARY_PATH
echo "--make end--"
make -j$(nproc) install

mkdir -p $installpath/AUGUSTUS/Augustus-3.4.0 && cp -r $buildpath/Augustus-3.4.0/* $installpath/AUGUSTUS/Augustus-3.4.0

cat>"$installpath/AUGUSTUS/Augustus-3.4.0/Augustus_modulefiles"<<EOF
#%Module1.0
conflict Augustus
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set Augustus \$pwd
setenv Augustus \$Augustus
prepend-path PATH \$Augustus/bin
EOF

rm -rf $buildpath/Augustus-3.4.0
